{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-19T12:51:25.242308Z",
     "start_time": "2020-10-19T12:51:24.402488Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "import numpy as np\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-19T13:10:30.487262Z",
     "start_time": "2020-10-19T13:10:30.483128Z"
    }
   },
   "outputs": [],
   "source": [
    "from torchvision import transforms,datasets\n",
    "from torch.utils.data import DataLoader\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-19T13:22:37.314806Z",
     "start_time": "2020-10-19T13:22:37.004480Z"
    }
   },
   "outputs": [],
   "source": [
    "batch_size=64\n",
    "transform = transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.1307,),(0.3081,))\n",
    "])\n",
    "\n",
    "train_dataset=datasets.MNIST(root='./dataset/mnist',\n",
    "                            train=True,\n",
    "                            download=True,\n",
    "                            transform=transform)\n",
    "train_loader=DataLoader(train_dataset,\n",
    "                       shuffle=True,\n",
    "                       batch_size=batch_size)\n",
    "test_dataset=datasets.MNIST(root='./dataset/mnist',\n",
    "                           train=False,\n",
    "                           download=True,\n",
    "                           transform=transform)\n",
    "test_loader=DataLoader(test_dataset,\n",
    "                      shuffle=False,\n",
    "                      batch_size=batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-19T13:28:18.050133Z",
     "start_time": "2020-10-19T13:28:18.033711Z"
    }
   },
   "outputs": [],
   "source": [
    "class Net(torch.nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Net,self).__init__()\n",
    "        self.l1=torch.nn.Linear(784,512)\n",
    "        self.l2=torch.nn.Linear(512,256)\n",
    "        self.l3=torch.nn.Linear(256,128)\n",
    "        self.l4=torch.nn.Linear(128,64)\n",
    "        self.l5=torch.nn.Linear(64,10)\n",
    "    def forward(self,x):\n",
    "        x=x.view(-1,784)\n",
    "        o1=F.relu(self.l1(x))\n",
    "        o2=F.relu(self.l2(o1))\n",
    "        o3=F.relu(self.l3(o2))\n",
    "        o4=F.relu(self.l4(o3))\n",
    "        return self.l5(o4)\n",
    "    \n",
    "model=Net()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-19T13:29:56.491190Z",
     "start_time": "2020-10-19T13:29:56.482580Z"
    }
   },
   "outputs": [],
   "source": [
    "criterion=torch.nn.CrossEntropyLoss()\n",
    "optimizer=optim.SGD(model.parameters(),lr=0.01,momentum=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-19T13:34:33.131648Z",
     "start_time": "2020-10-19T13:34:33.125286Z"
    }
   },
   "outputs": [],
   "source": [
    "def train(epoch):\n",
    "    running_loss=0.\n",
    "    for batch_index,data in enumerate(train_loader,0):\n",
    "        inputs,target=data\n",
    "        optimizer.zero_grad()\n",
    "        \n",
    "        outputs=model(inputs)\n",
    "        loss=criterion(outputs,target)\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        \n",
    "        running_loss+=loss.item()\n",
    "        if batch_index %300==299:\n",
    "            print('[%d,%5d] loss:%.3f '% (epoch +1, batch_index+1,running_loss/300))\n",
    "            running_loss=0.0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-19T13:39:30.744699Z",
     "start_time": "2020-10-19T13:39:30.738193Z"
    }
   },
   "outputs": [],
   "source": [
    "def test():\n",
    "    correct=0\n",
    "    total=0\n",
    "    with torch.no_grad():\n",
    "        for data in test_loader:\n",
    "            images,labels =data\n",
    "            outputs = model(images)\n",
    "            _,predicted=torch.max(outputs.data,dim=1)\n",
    "            total+=labels.size(0)\n",
    "            correct+=(predicted==labels).sum().item()\n",
    "    print('Accuracy on test set: %d %%' % (100*correct/total))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-19T14:09:03.212218Z",
     "start_time": "2020-10-19T14:05:43.814412Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1,  300] loss:0.026 \n",
      "[1,  600] loss:0.029 \n",
      "[1,  900] loss:0.027 \n",
      "Accuracy on test set: 97 %\n",
      "[2,  300] loss:0.016 \n",
      "[2,  600] loss:0.023 \n",
      "[2,  900] loss:0.025 \n",
      "Accuracy on test set: 97 %\n",
      "[3,  300] loss:0.017 \n",
      "[3,  600] loss:0.016 \n",
      "[3,  900] loss:0.016 \n",
      "Accuracy on test set: 97 %\n",
      "[4,  300] loss:0.011 \n",
      "[4,  600] loss:0.012 \n",
      "[4,  900] loss:0.015 \n",
      "Accuracy on test set: 97 %\n",
      "[5,  300] loss:0.010 \n",
      "[5,  600] loss:0.009 \n",
      "[5,  900] loss:0.012 \n",
      "Accuracy on test set: 97 %\n",
      "[6,  300] loss:0.006 \n",
      "[6,  600] loss:0.009 \n",
      "[6,  900] loss:0.010 \n",
      "Accuracy on test set: 97 %\n",
      "[7,  300] loss:0.006 \n",
      "[7,  600] loss:0.006 \n",
      "[7,  900] loss:0.005 \n",
      "Accuracy on test set: 97 %\n",
      "[8,  300] loss:0.004 \n",
      "[8,  600] loss:0.004 \n",
      "[8,  900] loss:0.004 \n",
      "Accuracy on test set: 97 %\n",
      "[9,  300] loss:0.003 \n",
      "[9,  600] loss:0.003 \n",
      "[9,  900] loss:0.004 \n",
      "Accuracy on test set: 98 %\n",
      "[10,  300] loss:0.002 \n",
      "[10,  600] loss:0.002 \n",
      "[10,  900] loss:0.003 \n",
      "Accuracy on test set: 97 %\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    for epoch in range(10):\n",
    "        train(epoch)\n",
    "        test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
